package test.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import com.lzy.imagepicker.ImagePicker;
import com.lzy.imagepicker.bean.ImageItem;
import com.lzy.imagepicker.ui.ImagePreviewDelActivity;
import com.squareup.picasso.Picasso;
import java.io.File;
import java.util.ArrayList;
import org.an.ku.base.adapter.BaseListAdapter;
import org.an.proj.contacts.R;
import org.an.proj.contacts.config.LogUtil;
import photo.PhotoItem;

import static com.lzy.imagepicker.ImagePicker.REQUEST_CODE_PREVIEW;

/**
 * 创建时间：2017年03月23日19:53 <br>
 * 作者：fuchaoyang <br>
 * 描述：
 */

public class GridAdapter extends BaseListAdapter<PhotoItem> {
  private int mMaxCount;
  private int[] colors = {
    Color.RED, Color.BLUE, Color.GREEN
  };

  @Override public int getCount() {
    if (getDatas() != null && getDatas().size() == mMaxCount) {
      return mMaxCount;
    } else {
      return super.getCount() + 1;
    }
  }

  public GridAdapter(Context context, int maxCount) {
    super(context);
    this.mMaxCount = maxCount;
  }

  @Override public View getView(final int position, View convertView, ViewGroup parent) {
    View view = mInflater.inflate(R.layout.view_uploadimg, parent, false);
    ImageView imageView = (ImageView) view.findViewById(R.id.iv_upload);
    final boolean isAdd = position == getDatas().size();
    if (!isAdd) {
      getItem(position).imageView = imageView;
    }
    imageView.setOnClickListener(new View.OnClickListener() {
      @Override public void onClick(View v) {
        if (isAdd) {
          // 点击的是添加
          ImagePicker.getInstance().start((Activity) context, 303);
        }
      }
    });
    if (isAdd) {
      imageView.setImageResource(R.drawable.icon_add_photo);
    } else {
      Picasso.with(context)
        .load(Uri.fromFile(new File(getItem(position).path)))
        .centerCrop()
        .fit()
        .placeholder(R.drawable.img_defult)
        .into(imageView);
    }

    view.findViewById(R.id.rl_root).setBackgroundColor(colors[position % colors.length]);
    return view;
  }

  static interface OnPhotoHandleListener {
    void onPhotoClick(int position);

    void onAdd();

    void onDelete();
  }
}
